
/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#include <ngx_config.h>
#include <ngx_core.h>
#include <nginx.h>


ngx_int_t   ngx_ncpu;
ngx_int_t   ngx_max_sockets;
ngx_uint_t  ngx_inherited_nonblocking;
ngx_uint_t  ngx_tcp_nodelay_and_tcp_nopush;


struct rlimit  rlmt;


/*@brief:注册的系统接口;
 *
 *
 */
ngx_os_io_t ngx_os_io = {
    ngx_unix_recv,
    ngx_readv_chain,
    ngx_udp_unix_recv,
    ngx_unix_send,
    ngx_udp_unix_send,
    ngx_udp_unix_sendmsg_chain,
    ngx_writev_chain,
    0
};

/*@brief:nginx 针对于linux系统的初始化
 *@para:ngx_log_t *log:日志管理模块
 *
 *
 *@return value:
 */
ngx_int_t ngx_os_init(ngx_log_t *log)
{
	ngx_log_error(NGX_LOG_NOTICE, log, 0,"enter [function]{%s}###########################################!!!!!",__func__);
    ngx_time_t  *tp;
    ngx_uint_t   n;

#if (NGX_HAVE_OS_SPECIFIC_INIT)
    if (ngx_os_specific_init(log) != NGX_OK) {
        return NGX_ERROR;
    }
#endif

    if (ngx_init_setproctitle(log) != NGX_OK) {
        return NGX_ERROR;
    }

    ngx_pagesize = getpagesize(); //4096 
    ngx_cacheline_size = NGX_CPU_CACHE_LINE; //64 
	
    for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ }

#if (NGX_HAVE_SC_NPROCESSORS_ONLN)
    if (ngx_ncpu == 0) {
        ngx_ncpu = sysconf(_SC_NPROCESSORS_ONLN); // 2个;
    }
#endif

    if (ngx_ncpu < 1) {
        ngx_ncpu = 1;
    }
	ngx_log_error(NGX_LOG_NOTICE, log, 0,"[function]{%s} get cpu num:%d pagesize:%d ngx_cacheline_size:%d", __func__,ngx_ncpu, ngx_pagesize, ngx_cacheline_size);
    ngx_cpuinfo();

	//获取当前系统的配置RLIMIT_NOFILE 是打开最多的文件描述符数
    if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
        ngx_log_error(NGX_LOG_ALERT, log, errno, "getrlimit(RLIMIT_NOFILE) failed");
        return NGX_ERROR;
    }

    ngx_max_sockets = (ngx_int_t) rlmt.rlim_cur;

#if (NGX_HAVE_INHERITED_NONBLOCK || NGX_HAVE_ACCEPT4)
    ngx_inherited_nonblocking = 1;
#else
    ngx_inherited_nonblocking = 0;
#endif
    tp = ngx_timeofday();
    srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);
	ngx_log_error(NGX_LOG_NOTICE, log, 0,"leave [function]{%s}###########################################!!!!!",__func__);
    return NGX_OK;
}

/*@brief:
 *@para:ngx_log_t *log: 日志管理模块 
 *
 *
 *@return value:
 */
void ngx_os_status(ngx_log_t *log)
{
    ngx_log_error(NGX_LOG_NOTICE, log, 0, NGINX_VER_BUILD);

#ifdef NGX_COMPILER
    ngx_log_error(NGX_LOG_NOTICE, log, 0, "built by " NGX_COMPILER);
#endif

#if (NGX_HAVE_OS_SPECIFIC_INIT)
    ngx_os_specific_status(log);
#endif
    ngx_log_error(NGX_LOG_NOTICE, log, 0, "getrlimit(RLIMIT_NOFILE): %r:%r", rlmt.rlim_cur, rlmt.rlim_max);
}


